<template>
  <div class="siteProgram">
    <div class="table-page-search-wrapper">
      <p-form layout="inline" @keyup.enter.native="searchQuery">
        <p-row :gutter="24">
          <p-col :xxl="5" :xl="6">
            <p-form-item label="程序编码">
              <p-input placeholder="请输入内容" v-model="queryParamSelf.procedureCode"></p-input>
            </p-form-item>
          </p-col>
          <p-col :xxl="5" :xl="6">
            <p-form-item label="程序版本">
              <p-input placeholder="请输入内容" v-model="queryParamSelf.procedureVersion"></p-input>
            </p-form-item>
          </p-col>
          <p-col :xxl="5" :xl="6">
            <p-form-item label="程序类型">
              <p-input placeholder="请输入内容" v-model="queryParamSelf.procedureType"></p-input>
            </p-form-item>
          </p-col>

          <p-col :xxl="5" :xl="6">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <p-button type="primary" @click="searchQuerySelf" icon="search">查询</p-button>
              <p-button type="primary" @click="searchResetSelf" icon="reload" style="margin-left: 8px">重置</p-button>
            </span>
          </p-col>
        </p-row>
      </p-form>
    </div>
    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <p-button @click="handleAddShkill" type="primary" icon="plus">新增</p-button>
       <p-button :disabled="!selectedRowKeys.length > 0" @click="batchDelSkill" style="margin-left: 8px">
          <p-icon type="delete" />删除 
       </p-button>
    </div>
    <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
      <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
      <a style="margin-left: 24px" @click="onClearSelected">清空</a>

      <span style="float:right;">
        <!-- <a @click="loadData()"><p-icon type="sync" />刷新</a> -->
        <p-divider type="vertical" />
          <p-popover title="自定义列" trigger="click" placement="leftBottom"  :overlay-style="getWidth">
          <template slot="content">
            <p-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
              <p-row>
                <template v-for="(item,index) in defColumns">
                  <template v-if="item.key!='rowIndex'&& item.dataIndex!='action'">
                      <p-col :span="12"><p-checkbox :value="item.dataIndex">{{ item.title }}</p-checkbox></p-col>
                  </template>
                </template>
              </p-row>
            </p-checkbox-group>
          </template>
          <a><p-icon type="setting" />自定义列</a>
        </p-popover>
      </span>
    </div>
    <p-table
      ref="table"
      size="middle"
      bordered
      :rowKey="idField"
      :columns="columns"
      :dataSource="options"
      :pagination="false"
      :loading="loading"
      :rowSelection="{fixed:true,selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
      @change="handleTableChange">
        <span slot="action" slot-scope="text, record, index">
          <p-popconfirm title="确定删除吗?" @confirm="() => handleDeleteSkill(index)">
            <a>删除</a>
          </p-popconfirm>
        </span>
    </p-table>
    <tableChoose 
      @confirm="tableChooseConfirm" 
      tag-name="procedureName" 
      ref="tableChoose"
      :inputText="'请输入程序编码或名称'"
      :title="'选择程序'"
      :idField="idField"
      :apiMethods="'getMesPeProcedure'"
      :columns="tableChooseCols" 
      :x="1200"
    />
  </div>
</template>

<script>
import Vue from 'vue'
import mesPeProcess from "@/api/mesPeProcess";
import JSuperQuery from '@/components/jeecg/JSuperQuery.vue'
import tableChoose from '@/components/tableChoose/index.vue'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import _ from "lodash";
export default {
  name: "siteSkill",
  mixins:[JeecgListMixin],
  components: {
    JSuperQuery,
    tableChoose,
  },
  props: {
    options: {
      type: Array,
      default: []
    },
    siteIndex: Object,
  },
  data() {
    return {
      idField: 'procedureCode',
      searchQuery: '',
      tableChooseCols: [
        {
          title: '序号',
          dataIndex: '',
          key:'rowIndex',
          width:60,
          fixed: 'left',
          align: 'center',
          customRender: function (t,r,index) {
            return parseInt(index)+1;
          }
        },
        { 
          title: '程序编码',
          ellipsis: true, 
          dataIndex: 'procedureCode', 
        },
        { 
          title: '程序名称',
          ellipsis: true,  
          dataIndex: 'procedureName', 
        },
        { 
          title: '程序版本',
          ellipsis: true,  
          dataIndex: 'procedureVersion', 
        },
        { 
          title: '程序类型',
          ellipsis: true,  
          dataIndex: 'procedureType', 
        },
        { 
          title: '备注',
          ellipsis: true,  
          dataIndex: 'remark', 
        },
        { 
          title: '事业部',
          ellipsis: true,  
          dataIndex: 'businessDivision', 
        },
        { 
          title: '发出时间',
          ellipsis: true, 
          width: 160,
          dataIndex: 'sendTime', 
        },
        { 
          title: '接收部门',
          ellipsis: true,  
          dataIndex: 'dptname', 
        },
        { 
          title: '拟制',
          ellipsis: true,
          dataIndex: 'createBy', 
        },
        { 
          title: '修改',
          ellipsis: true, 
          dataIndex: 'updateBy', 
        }
      ],
      queryParamSelf: {
        procedureCode: '',
        procedureVersion: '',
        procedureType: '',
      },
      optionsCopy: [],
      options: [],
      // 表头
      columns: [
      ],
      //列设置
      settingColumns:[],
      //列定义
      defColumns: [
        {
          title: '序号',
          dataIndex: '',
          key:'rowIndex',
          width:60,
          align: 'center',
          customRender: function (t,r,index) {
            return parseInt(index)+1;
          }
        },
        { 
          title: '程序编码', 
          dataIndex: 'procedureCode', 
        },
        { 
          title: '程序名称', 
          dataIndex: 'procedureName', 
        },
        { 
          title: '程序名称', 
          dataIndex: 'procedureVersion', 
        },
        { 
          title: '程序类型', 
          dataIndex: 'procedureType', 
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          width:110,
          fixed: 'right',
          scopedSlots: { customRender: 'action' }
        }
      ],
      /* url: {
        list: '/mes-craft/mesPeSkill/list',
      }, */
    };
  },
  computed: {
  },
  created() {
    this.initColumns();
    this.optionsCopy = _.cloneDeep(this.options);
    // this.getMesPeProcedure();
  },
  mounted(){
    // this.getMesHrSkillBank();
    this.getMesPeProcedure();
    // this.$refs.tableChoose.columns = this.tableChooseCols;
  },
  methods: {
    searchQuery(){},
    delShill(){},
    updateOption(){
      this.$store.commit('setCurNodeData', {
        ...this.siteIndex,
        options: this.options,
        field: 'gramData',
      });
      this.optionsCopy = _.cloneDeep(this.options);
    },
    getMesPeProcedure(param){
      mesPeProcess.getMesPeProcedure(param).then( res =>{
        if ( res.success ){
          this.$refs.tableChoose.tableData = res.data.records || [];
          this.$refs.tableChoose.pagination.total = res.data.total;
        }
      })
    },
    searchQuerySelf(){
      const that = this;
      that.options = that.optionsCopy.filter( el =>{
        const procedureCode = that.queryParamSelf.procedureCode || el.procedureCode;
        const procedureVersion = that.queryParamSelf.procedureVersion || el.procedureVersion;
        const procedureType = that.queryParamSelf.procedureType || el.procedureType;
        return (el.procedureCode || '').includes(procedureCode) && (el.procedureVersion||'').includes(procedureVersion) && (el.procedureType||'').includes(procedureType)
      });
    },
    searchResetSelf(){
      this.queryParamSelf.procedureCode = ''; 
      this.queryParamSelf.procedureVersion = ''; 
      this.queryParamSelf.procedureType = '';
      this.options = _.cloneDeep(this.optionsCopy);
    },
    tableChooseConfirm(item){
      // 并集并去重
      const arr = this.options.concat(item);
      this.options = _.uniqBy(arr,this.idField);
      this.updateOption();
    },
    handleAddShkill(){
      this.$refs.tableChoose.visible = true;
      this.$refs.tableChoose.selectedRowKeys = this.options.map( el => el[this.idField]);
    },
    getWidth:function(){
      if(this.defColumns.length > 14){
        return {width : '500px'}
      }
    },
    batchDelSkill(){
      this.$confirm({
        title: '确认删除',
        content: '是否删除选中数据?',
        onOk: function() {
          this.options = this.options.filter( el =>{
            return !this.selectedRowKeys.includes(el[this.idField] )
          });
          that.updateOption();
        }
      })
    },
    handleDeleteSkill(index){
      this.options.splice(index, 1);
      this.updateOption();
    },
    //列设置更改事件
    onColSettingsChange (checkedValues) {
      var key = this.$route.name+":colsettings";
      Vue.ls.set(key, checkedValues, 7 * 24 * 60 * 60 * 1000)
      this.settingColumns = checkedValues;
      const cols = this.defColumns.filter(item => {
        if(item.key =='rowIndex'|| item.dataIndex=='action'){
          return true
        }
        if (this.settingColumns.includes(item.dataIndex)) {
          return true
        }
        return false
      })
      this.columns =  cols;
    },
    initColumns(){
        this.columns =  this.defColumns;
        this.settingColumns =  this.defColumns.map(function (item) {
          return item.dataIndex;
        });
      }
  },
};
</script>
<style scoped lang='less'>
.siteProgram{
}
</style>